﻿using Lhz.Common.Business;
using Lhz.Data.Linq.Nh;

namespace Lhz.Test.Common
{
    public class ProxyCrudTester<TPk, TEntity> : CrudTester<TPk, TEntity> where TEntity : class
    {
        private EntityProxy<TPk, TEntity> entityProxy;

        public ProxyCrudTester(EntityProxy<TPk, TEntity> entityProxy = null)
        {
            if (entityProxy == null)
            {
                this.entityProxy = new EntityProxy<TPk, TEntity>();
            }
            else
            {
                this.entityProxy = entityProxy;
            }
        }

        protected override TEntity QueryByPk(TPk pk, params string[] paths)
        {
            entityProxy.ClearCache();
            return entityProxy.QueryByPk(pk).LoadByPathsAndClearProxyAndRestoreProxyData(paths);
        }

        protected override void Add(TEntity entity)
        {
            entityProxy.Add(entity);
        }

        protected override void Update(TEntity entity)
        {
            entityProxy.Update(entity);
        }

        protected override void Delete(TEntity entity)
        {
            entityProxy.Delete(entity);
        }
    }
}
